<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${packageName}.dao.${IDaoName}">

	<insert id="add" parameterType="java.util.HashMap">
		<#-- 如果数据库有主键并且是递增，则传过来的list没有主键 -->
		<#if dataBaseType == "mysql">
		insert into `${realTableName}`
		</#if>
		<#if dataBaseType == "oracle">
		insert into "${realTableName}"
		</#if> 
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<#list columnList as data>
				<#-- 最后一项不加逗号 -->
				<if test="${data.columnsEng} != null">
					${data.columnsEng},
				</if>
			</#list>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<#list columnList as data>
				<if test="${data.columnsEng} != null">
					<#noparse>#{</#noparse>${data.columnsEng}<#noparse>}</#noparse>,
				</if>
			</#list>
		</trim>
	</insert>

	<delete id="delete" parameterType="java.util.HashMap">
		<#if dataBaseType == "mysql">
		delete from `${realTableName}`
		</#if>
		<#if dataBaseType == "oracle">
		delete from "${realTableName}"
		</#if>
		<where>
		<#list primaryKeyList as primaryKey>
				<#-- 最后一项不加逗号 -->
				<if test="${primaryKey} != null">
					and ${primaryKey}=<#noparse>#{</#noparse>${primaryKey}<#noparse>}</#noparse>
				</if>
		</#list>
		</where>
	</delete>

	<update id="update" parameterType="java.util.HashMap">
		<#if dataBaseType == "mysql">
		update `${realTableName}`
		</#if>
		<#if dataBaseType == "oracle">
		update "${realTableName}"
		</#if>
		<trim prefix="set" suffixOverrides=",">
			<#list columnList as data>
				<#-- 最后一项不加逗号 -->
				<if test="${data.columnsEng} != null">
					${data.columnsEng}=<#noparse>#{</#noparse>${data.columnsEng}<#noparse>}</#noparse>,
				</if>
			</#list>
		</trim>
		<where>
		<#list primaryKeyList as primaryKey>
				<#-- 最后一项不加逗号 -->
				<if test="${primaryKey} != null">
					and ${primaryKey}=<#noparse>#{</#noparse>${primaryKey}<#noparse>}</#noparse>
				</if>
		</#list>
		</where>
	</update>

	<select id="select" parameterType="java.util.HashMap"
		resultType="java.util.LinkedHashMap">
		<#if dataBaseType == "mysql">
			select 
			<#list selectColumnListMapper as data>
				${data.columnsEng}<#if data_has_next>,</#if>
			</#list>
			from `${realTableName}`
			
			<where>
	
				<#list columnList as data>
					<#-- 最后一项不加逗号 -->
					<if test="${data.columnsEng} != null">
						and ${data.columnsEng}=<#noparse>#{</#noparse>${data.columnsEng}<#noparse>}</#noparse>
					</if>
				</#list>
	
			</where>
			<if test="orderStr != '' and orderStr != null">
				order by <#noparse>${</#noparse>orderStr<#noparse>}</#noparse>
			</if>
			<if test="start != null and pageSize != null">
				limit <#noparse>#{</#noparse>start<#noparse>}</#noparse>,<#noparse>#{</#noparse>pageSize<#noparse>}</#noparse>
			</if>
		</#if>
		
		<#if dataBaseType == "oracle">
			SELECT
			<#list selectColumnListMapper as data>
				${data.columnsEng}<#if data_has_next>,</#if>
			</#list>
			<if test="startIndex == null or endIndex == null">
				FROM "${realTableName}"
	                  <where>
						<#list columnList as data>
							<#-- 最后一项不加逗号 -->
							<if test="${data.columnsEng} != null">
								and ${data.columnsEng}=<#noparse>#{</#noparse>${data.columnsEng}<#noparse>}</#noparse>
							</if>
						</#list>
					  </where>
					  <if test="orderStr != '' and orderStr != null">
						order by <#noparse>${</#noparse>orderStr<#noparse>}</#noparse>
					  </if>
			</if>
			<if test="startIndex != null and endIndex != null">
			
	  			FROM (SELECT t2.*, ROWNUM rowno
	
	          	FROM (SELECT t1.*
	
	                  FROM "${realTableName}" t1
	
	                  <where>
		
						<#list columnList as data>
							<#-- 最后一项不加逗号 -->
							<if test="${data.columnsEng} != null">
								and ${data.columnsEng}=<#noparse>#{</#noparse>${data.columnsEng}<#noparse>}</#noparse>
							</if>
						</#list>
						
					  </where>
	
	                  <if test="orderStr != '' and orderStr != null">
						order by <#noparse>${</#noparse>orderStr<#noparse>}</#noparse>
					  </if>
	                  
	                  ) t2
	
	           <![CDATA[WHERE ROWNUM <= <#noparse>#{</#noparse>endIndex<#noparse>}</#noparse>) t3
	
	 		   WHERE t3.rowno >= <#noparse>#{</#noparse>startIndex<#noparse>}</#noparse>]]>
	 		</if>
		</#if>
		
	</select>
	
	<select id="likeSelect" parameterType="java.util.HashMap"
		resultType="java.util.LinkedHashMap">
		<#if dataBaseType == "mysql">
			select
			
			<#list selectColumnListMapper as data>
				${data.columnsEng}<#if data_has_next>,</#if>
			</#list>
			from `${realTableName}`
			
			<where>
	
				<#list queryColumnList as data>
					<#if data.compareValue==">= && <=">
						<if test="start${data.columnsEng} != null and start${data.columnsEng} != '' ">
						and ${data.columnsEng} <![CDATA[>=]]> <#noparse>#{</#noparse>start${data.columnsEng}<#noparse>}</#noparse>
						</if>
						<if test="end${data.columnsEng} != null and end${data.columnsEng} != '' ">
						and ${data.columnsEng} <![CDATA[<=]]> <#noparse>#{</#noparse>end${data.columnsEng}<#noparse>}</#noparse>
						</if>
					<#else>
						<if test="${data.columnsEng} != null and ${data.columnsEng} != '' ">
						<#if data.compareValue=="like">
						and ${data.columnsEng} like <#noparse>"%"#{</#noparse>${data.columnsEng}<#noparse>}"%"</#noparse>
						<#else>
						and ${data.columnsEng} <![CDATA[${data.compareValue}]]> <#noparse>#{</#noparse>${data.columnsEng}<#noparse>}</#noparse>
						</#if>
						</if>
					</#if>
				</#list>
	
			</where>
			
			<if test="orderStr != '' and orderStr != null">
				order by <#noparse>${</#noparse>orderStr<#noparse>}</#noparse>
			</if>
			<if test="start != null and pageSize != null">
				limit <#noparse>#{</#noparse>start<#noparse>}</#noparse>,<#noparse>#{</#noparse>pageSize<#noparse>}</#noparse>
			</if>
		</#if>
		
		<#if dataBaseType == "oracle">
			SELECT
			<#list selectColumnListMapper as data>
				${data.columnsEng}<#if data_has_next>,</#if>
			</#list>
			<if test="startIndex == null or endIndex == null">
				FROM "${realTableName}"
	                  <where>
						<#list queryColumnList as data>
							<#if data.compareValue==">= && <=">
							<if test="start${data.columnsEng} != null and start${data.columnsEng} != '' ">
							and ${data.columnsEng} <![CDATA[>=]]> <#noparse>#{</#noparse>start${data.columnsEng}<#noparse>}</#noparse>
							</if>
							<if test="end${data.columnsEng} != null and end${data.columnsEng} != '' ">
							and ${data.columnsEng} <![CDATA[<=]]> <#noparse>#{</#noparse>end${data.columnsEng}<#noparse>}</#noparse>
							</if>
							<#else>
							<if test="${data.columnsEng} != null and ${data.columnsEng} != '' ">
							<#if data.compareValue=="like">
							and ${data.columnsEng} like <#noparse>"%"#{</#noparse>${data.columnsEng}<#noparse>}"%"</#noparse>
							<#else>
							and ${data.columnsEng} <![CDATA[${data.compareValue}]]> <#noparse>#{</#noparse>${data.columnsEng}<#noparse>}</#noparse>
							</#if>
							</if>
							</#if>
						</#list>
					  </where>
					  <if test="orderStr != '' and orderStr != null">
						order by <#noparse>${</#noparse>orderStr<#noparse>}</#noparse>
					  </if>
			</if>
			<if test="startIndex != null and endIndex != null">
	  			FROM (SELECT t2.*, ROWNUM rowno
	
	          	FROM (SELECT t1.*
	
	                  FROM "${realTableName}" t1
	
	                  <where>
		
						<#list queryColumnList as data>
							<#if data.compareValue==">= && <=">
							<if test="start${data.columnsEng} != null and start${data.columnsEng} != '' ">
							and ${data.columnsEng} <![CDATA[>=]]> <#noparse>#{</#noparse>start${data.columnsEng}<#noparse>}</#noparse>
							</if>
							<if test="end${data.columnsEng} != null and end${data.columnsEng} != '' ">
							and ${data.columnsEng} <![CDATA[<=]]> <#noparse>#{</#noparse>end${data.columnsEng}<#noparse>}</#noparse>
							</if>
							<#else>
							<if test="${data.columnsEng} != null and ${data.columnsEng} != '' ">
							<#if data.compareValue=="like">
							and ${data.columnsEng} like <#noparse>"%"#{</#noparse>${data.columnsEng}<#noparse>}"%"</#noparse>
							<#else>
							and ${data.columnsEng} <![CDATA[${data.compareValue}]]> <#noparse>#{</#noparse>${data.columnsEng}<#noparse>}</#noparse>
							</#if>
							</if>
							</#if>
						</#list>
						
					  </where>
	
	                  <if test="orderStr != '' and orderStr != null">
						order by <#noparse>${</#noparse>orderStr<#noparse>}</#noparse>
					  </if>
	                  
	                  ) t2

	           <![CDATA[WHERE ROWNUM <= <#noparse>#{</#noparse>endIndex<#noparse>}</#noparse>) t3
	
	 		   WHERE t3.rowno >= <#noparse>#{</#noparse>startIndex<#noparse>}</#noparse>]]>
 		   </if>
		</#if>
		
	</select>
	
	<select id="likeSelectCount" parameterType="java.util.HashMap"
		resultType="java.lang.Long">
		<#if dataBaseType == "mysql">
		select 
			count(1)
		from `${realTableName}`
		</#if>
		<#if dataBaseType == "oracle">
		select 
			count(1)
		from "${realTableName}"
		</#if>
		
		<where>

			<#list queryColumnList as data>
				<#if data.compareValue==">= && <=">
				<if test="start${data.columnsEng} != null and start${data.columnsEng} != '' ">
				and ${data.columnsEng} <![CDATA[>=]]> <#noparse>#{</#noparse>start${data.columnsEng}<#noparse>}</#noparse>
				</if>
				<if test="end${data.columnsEng} != null and end${data.columnsEng} != '' ">
				and ${data.columnsEng} <![CDATA[<=]]> <#noparse>#{</#noparse>end${data.columnsEng}<#noparse>}</#noparse>
				</if>
				<#else>
				<if test="${data.columnsEng} != null and ${data.columnsEng} != '' ">
				<#if data.compareValue=="like">
				and ${data.columnsEng} like <#noparse>"%"#{</#noparse>${data.columnsEng}<#noparse>}"%"</#noparse>
				<#else>
				and ${data.columnsEng} <![CDATA[${data.compareValue}]]> <#noparse>#{</#noparse>${data.columnsEng}<#noparse>}</#noparse>
				</#if>
				</if>
				</#if>
			</#list>
			
		</where>
	</select>
	
</mapper>